//
//  UIBarButtonItem + Extension.swift
//  Demo1
//
//  Created by 余仿 on 16/4/9.
//  Copyright © 2016年 YF. All rights reserved.
//


import UIKit

// MARK: - 类扩展
extension UIBarButtonItem{

    /*Designated initializer cannot be declared in an extension of 'UIBarButtonItem'; did you mean this to be a convenience initializer?*/
    //Designated 专门设计的构造函数 ,   -> 指定的构造函数
// 指定的构造函数 和 存储性的属性 都不能够在扩展中声明
    /*
     Convenience initializer for 'UIBarButtonItem' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super.init')*/
    /* Extensions may not contain stored properties存储性的属性*/

     convenience init(imageName:String?,title:String?,target:AnyObject?,action:Selector?) {

        self.init()

        let btn = UIButton()

        if let imgName = imageName{

            btn.setImage(UIImage(named: imgName + "_highlighted"), forState: .Highlighted)

            btn.setImage(UIImage(named: imgName), forState: .Normal)

            btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 5)

        }

        btn.setTitle(title, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)


        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)

        if let act = action{

            btn.addTarget(target, action: act, forControlEvents: .TouchUpInside)

        }

        btn.sizeToFit()

        customView = btn

    }
}